package com.xujl.mvc;

import cn.hutool.core.io.FileUtil;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.DataKeys;
import com.intellij.openapi.vfs.VirtualFile;
import org.jetbrains.annotations.NotNull;

import java.awt.*;

public class SwingAction extends AnAction {
    @Override
    public void actionPerformed(@NotNull AnActionEvent anActionEvent) {

        TestUI dialog = new TestUI(anActionEvent);

        Dimension screensize = Toolkit.getDefaultToolkit().getScreenSize();
        int x = (int) screensize.getWidth() / 2 - dialog.getPreferredSize().width / 2;
        int y = (int) screensize.getHeight() / 2 - dialog.getPreferredSize().height / 2;
        dialog.setTitle("MVC自动生成小工具");
        dialog.setLocation(x, y);


        dialog.pack();
        dialog.setVisible(true);


    }

    @Override
    public void update(@NotNull AnActionEvent e) {
//        super.update(e);
        try {
            VirtualFile selectGroup = DataKeys.VIRTUAL_FILE.getData(e.getDataContext());
            String path = selectGroup.getPath();
            if (FileUtil.isDirectory(path)) {
                e.getPresentation().setVisible(true);
            } else {
                e.getPresentation().setVisible(false);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }

    }
}
